package com.smartleanx.module.hrm.controller.app.employee;

import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import com.smartleanx.framework.security.core.annotations.PreAuthenticated;
import com.smartleanx.module.hrm.context.EmployeeContextHolder;
import com.smartleanx.module.hrm.controller.admin.employee.certificate.EmployeeCertificateRespVO;
import com.smartleanx.module.hrm.controller.admin.employee.certificate.EmployeeCertificateSaveReqVO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeCertificateDO;
import com.smartleanx.module.hrm.service.employee.EmployeeCertificateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.smartleanx.framework.common.pojo.CommonResult.success;

@Tag(name = "用户APP - 员工证书")
@RestController
@RequestMapping("/hrm/employee-certificate")
@Validated
public class APPEmployeeCertificateController {

    @Resource
    private EmployeeCertificateService employeeCertificateService;

    @PostMapping("/create")
    @Operation(summary = "创建员工证书")
    @PreAuthenticated
    public CommonResult<Long> createEmployeeCertificate(@Valid @RequestBody EmployeeCertificateSaveReqVO createReqVO) {
        return success(employeeCertificateService.createEmployeeCertificate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新员工证书")
    @PreAuthenticated
    public CommonResult<Boolean> updateEmployeeCertificate(@Valid @RequestBody EmployeeCertificateSaveReqVO updateReqVO) {
        employeeCertificateService.updateEmployeeCertificate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除员工证书")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthenticated
    public CommonResult<Boolean> deleteEmployeeCertificate(@RequestParam("id") Long id) {
        employeeCertificateService.deleteEmployeeCertificate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得员工证书")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<EmployeeCertificateRespVO> getEmployeeCertificate(@RequestParam("id") Long id) {
        EmployeeCertificateDO employeeCertificate = employeeCertificateService.getEmployeeCertificate(id);
        return success(BeanUtils.toBean(employeeCertificate, EmployeeCertificateRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得员工证书列表")
    @PreAuthenticated
    public CommonResult<List<EmployeeCertificateRespVO>> getEmployeeCertificateList(){
        Long employeeId = EmployeeContextHolder.getEmployeeId();
        List<EmployeeCertificateDO> certificateList = employeeCertificateService.getEmployeeCertificateList(employeeId);
        return success(BeanUtils.toBean(certificateList, EmployeeCertificateRespVO.class));
    }
}
